﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ImageTools;
using nyxwin.Main.Utils;

namespace nyxwin.Main.Controls
{
    public partial class NyxIcon : UserControl
    {
        public NyxIcon()
        {
            InitializeComponent();

            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(Application.Current.RootVisual))
            {
                ExtendedImage img = new ExtendedImage();
                img.UriSource = new Uri("/Icons/nyxicobg.gif", UriKind.Relative);
                bgImage.Source = img;
            }
        }

        public string Uri
        {
            get { return (string)GetValue(UriProperty); }
            set { SetValue(UriProperty, value); }
        }

        public static readonly DependencyProperty UriProperty =
            DependencyProperty.Register("Uri", typeof(string), typeof(NyxIcon), new PropertyMetadata(null, new PropertyChangedCallback(
                (obj, args) =>
                {
                    var icon = obj as NyxIcon;

                    if (icon != null)
                    {
                        icon.SetUri(args.NewValue as string);
                    }
                }
                )));

        private void SetUri(string uri)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(Application.Current.RootVisual))
            {
                IconCache.Instance.GetStream(uri, x =>
                {
                    ExtendedImage ei = new ExtendedImage();

                    if (x != null)
                    {
                        ei.SetSource(x);
                    }

                    fgImage.Source = ei;
                });
            }
        }
    }
}
